package com.aat.dxfy.http.login;

import java.io.IOException;
import java.util.*;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class CookieDemo {
	public static BasicCookieStore cookieStore = new BasicCookieStore();
	public static  CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();

	public static void main(String[] args) throws IOException {
		login();
		delenv();
		httpClient.close();
	}
	
	public static void delenv()  throws IOException {
		//
		HttpGet httpGet = new HttpGet("http://127.0.0.1:56789/api/env/delete?id=5");

		CloseableHttpResponse response = httpClient.execute(httpGet);


		HttpEntity responseEntity = response.getEntity();

		String res = EntityUtils.toString(responseEntity, "UTF-8");

		System.out.println("\ndelapp:\n\t"+res);

		EntityUtils.consume(responseEntity);
		
		//
		List<Cookie> cookies = cookieStore.getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }
		//

		response.close();

		//
	}
	
	public static void delApp()  throws IOException {
		//
		HttpGet httpGet = new HttpGet("http://127.0.0.1:56789/api/app/delete?id=5");

		CloseableHttpResponse response = httpClient.execute(httpGet);


		HttpEntity responseEntity = response.getEntity();

		String res = EntityUtils.toString(responseEntity, "UTF-8");

		System.out.println("\ndelapp:\n\t"+res);

		EntityUtils.consume(responseEntity);
		
		//
		List<Cookie> cookies = cookieStore.getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }
		//

		response.close();

		//
	}
	
	public static void createApp()  throws IOException {
		//
		HttpPost httpPost = new HttpPost("http://127.0.0.1:56789/api/app");

		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("app", "haha"));
		nvps.add(new BasicNameValuePair("desc", "haha"));
		nvps.add(new BasicNameValuePair("emails", "haha@163.com"));

		httpPost.setEntity(new UrlEncodedFormEntity(nvps));

		CloseableHttpResponse response = httpClient.execute(httpPost);

		HttpEntity responseEntity = response.getEntity();

		String res = EntityUtils.toString(responseEntity, "UTF-8");

		System.out.println("\ncreate:\n\t"+res);

		EntityUtils.consume(responseEntity);
		
		//
		List<Cookie> cookies = cookieStore.getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }
		//

		response.close();

		//
	}

	public static void login()  throws IOException {
		//
		HttpPost httpPost = new HttpPost("http://127.0.0.1:56789/api/account/signin");

		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("name", "admin"));
		nvps.add(new BasicNameValuePair("password", "admin"));
		nvps.add(new BasicNameValuePair("remember", "0"));

		httpPost.setEntity(new UrlEncodedFormEntity(nvps));

		CloseableHttpResponse response = httpClient.execute(httpPost);

		HttpEntity responseEntity = response.getEntity();

		String res = EntityUtils.toString(responseEntity, "UTF-8");

		System.out.println("\nlogin:\n\t"+res);

		EntityUtils.consume(responseEntity);
		
		//
		List<Cookie> cookies = cookieStore.getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }
		//

		response.close();

		//
	}
	
	public static void session()  throws IOException {
		//
		HttpGet httpGet = new HttpGet("http://127.0.0.1:56789/api/account/session");

		CloseableHttpResponse response = httpClient.execute(httpGet);


		HttpEntity responseEntity = response.getEntity();

		String res = EntityUtils.toString(responseEntity, "UTF-8");

		System.out.println("\nsession:\n\t"+res);

		EntityUtils.consume(responseEntity);
		
		//
		List<Cookie> cookies = cookieStore.getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }
		//

		response.close();

		//
	}
	
	public static void signout()  throws IOException {
		//
		HttpGet httpGet = new HttpGet("http://127.0.0.1:56789/api/account/signout");

		CloseableHttpResponse response = httpClient.execute(httpGet);

		HttpEntity responseEntity = response.getEntity();

		String res = EntityUtils.toString(responseEntity, "UTF-8");

		System.out.println("\nsignout:\n\t"+res);

		EntityUtils.consume(responseEntity);
		
		//
		List<Cookie> cookies = cookieStore.getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }
		//

		response.close();

		//
	}

}
